package com.itheima.service.impl;

import com.itheima.common.utils.DateUtils;
import com.itheima.mapper.MemberBackendMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.ReportMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.*;
import com.itheima.service.ReportService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private MemberBackendMapper memberMapper;

    /**
     * 会员数量统计
     * @return
     */
    public MemberVO getMemberStatistics() {
        List<LocalDate> dateList = reportMapper.getMemberDate();
        MemberVO memberVO = new MemberVO();
        String date = StringUtils.join(dateList, ",");
        memberVO.setMonths(date);
        List<Integer> countList = reportMapper.getMemberStatistics();
        String count = StringUtils.join(countList, ",");
        memberVO.setMemberCount(count);
        return memberVO;
    }

    /**
     * 套餐数量统计
     * @return
     */
    public SetmealVO getSetmealStatistics() {
        List<Integer> setmealIds = orderMapper.getSetmealName();
        List<String> setmealName = new ArrayList<>();
        List<SetmealNumber> setmealCount = new ArrayList<>();
        for (Integer setmealId : setmealIds) {
            Setmeal setmeal = setmealMapper.getById(Long.valueOf(setmealId));
            setmealName.add(setmeal.getName());
            Integer count = orderMapper.getSetmealId(setmealId);
            SetmealNumber setmealNumber = new SetmealNumber(setmeal.getName(), count);
            setmealCount.add(setmealNumber);
        }
        String setmealNames = StringUtils.join(setmealName, ",");
        SetmealVO setmealVO = new SetmealVO(setmealNames,setmealCount);
        return setmealVO;
    }

    /**
     * 获取运营统计数据
     * @return
     */
    public BusinessReport businessStatistics() {
        try {
            LocalDate now = LocalDate.now();
            Date reportDate = DateUtils.parseString2Date(String.valueOf(now));
            String reportDate1 = DateUtils.parseDate2String(reportDate);
            Date beginWeek = DateUtils.getFirstDayOfWeek(reportDate);
            Date endWeek = DateUtils.getLastDayOfWeek(reportDate);
            Date beginMonth = DateUtils.getFirstDay4ThisMonth();
            Integer totalMember = memberMapper.getCount();
            Integer todayNewMember = memberMapper.getTodayNew(reportDate);
            Integer thisWeekNewMember = memberMapper.getWeekNew(beginWeek,endWeek);
            Integer thisMonthNewMember = memberMapper.getMonthNew(beginMonth,reportDate);
            Integer todayOrderNumber = orderMapper.getTodayNew(reportDate);
            Integer thisWeekOrderNumber = orderMapper.getWeekNew(beginWeek,endWeek);
            Integer thisMonthOrderNumber = orderMapper.getMonthNew(beginMonth,reportDate);
            Integer todayVisitsNumber = orderMapper.getTodayVisits(reportDate);
            Integer thisWeekVisitsNumber = orderMapper.getWeekVisits(beginWeek,endWeek);
            Integer thisMonthVisitsNumber = orderMapper.getMonthVisits(beginMonth,reportDate);
            List<HostSetmeal> hotSetmeal = new ArrayList<>();

            List<Integer> setmealIds = orderMapper.getSetmealName();
            for (Integer setmealId : setmealIds) {
                Setmeal setmeal = setmealMapper.getById(Long.valueOf(setmealId));
                Integer count = orderMapper.getSetmealId(setmealId);
                Integer proportion = count/orderMapper.getSum();
                HostSetmeal hostSetmeal1 = new HostSetmeal(proportion,setmeal.getName(),count);
                hotSetmeal.add(hostSetmeal1);
            }

            return new BusinessReport(reportDate1,totalMember,todayNewMember,thisWeekNewMember,thisMonthNewMember,
                    todayOrderNumber,thisWeekOrderNumber,thisMonthOrderNumber,todayVisitsNumber,thisWeekVisitsNumber,
                    thisMonthVisitsNumber,hotSetmeal);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 模板下载并填充数据
     * @param response
     */
    public void export(HttpServletResponse response) {
        BusinessReport businessReport = businessStatistics();
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/report_template.xlsx");
        try {
            XSSFWorkbook excel = new XSSFWorkbook(in);
            XSSFSheet sheet = excel.getSheet("Sheet1");
            sheet.getRow(2).getCell(5).setCellValue(businessReport.getReportDate());
            XSSFRow row = sheet.getRow(4);
            row.getCell(5).setCellValue(businessReport.getTodayNewMember());
            row.getCell(7).setCellValue(businessReport.getTotalMember());
            row = sheet.getRow(5);
            row.getCell(5).setCellValue(businessReport.getThisWeekNewMember());
            row.getCell(7).setCellValue(businessReport.getThisMonthNewMember());
            row = sheet.getRow(7);
            row.getCell(5).setCellValue(businessReport.getTodayOrderNumber());
            row.getCell(7).setCellValue(businessReport.getTodayVisitsNumber());
            row = sheet.getRow(8);
            row.getCell(5).setCellValue(businessReport.getThisWeekOrderNumber());
            row.getCell(7).setCellValue(businessReport.getThisWeekVisitsNumber());
            row = sheet.getRow(9);
            row.getCell(5).setCellValue(businessReport.getThisMonthOrderNumber());
            row.getCell(7).setCellValue(businessReport.getThisMonthVisitsNumber());
            int i = 12;
            for (HostSetmeal hostSetmeal : businessReport.getHotSetmeal()) {
                row = sheet.getRow(i);
                row.getCell(4).setCellValue(hostSetmeal.getName());
                row.getCell(5).setCellValue(hostSetmeal.getSetmeal_count());
                row.getCell(6).setCellValue(hostSetmeal.getProportion());
                row.getCell(7).setCellValue("无");
            }

            ServletOutputStream out = response.getOutputStream();
            excel.write(out);
            out.close();
            excel.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
